<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,200分)- 分积木（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>Solo和koko是两兄弟&#xff0c;妈妈给了他们一大堆积木&#xff0c;每块积木上都有自己的重量。现在他们想要将这些积木分成两堆。哥哥Solo负责分配&#xff0c;弟弟koko要求两个人获得的积木总重量“相等”&#xff08;根据Koko的逻辑&#xff09;&#xff0c;个数可以不同&#xff0c;不然就会哭&#xff0c;但koko只会先将两个数转成二进制再进行加法&#xff0c;而且总会忘记进位&#xff08;每个进位都忘记&#xff09;。如当25&#xff08;11101&#xff09;加11&#xff08;01011&#xff09;时&#xff0c;koko得到的计算结果是18&#xff08;10010&#xff09;&#xff1a;</p> 
<pre><code> 11001
&#43;01011
--------
 10010</code></pre> 
<p>Solo想要尽可能使自己得到的积木总重量最大&#xff0c;且不让koko哭。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行是一个整数N(2≤N≤100)&#xff0c;表示有多少块积木&#xff1b;</p> 
<p>第二行为空格分开的N个整数Ci(1≤Ci≤106)&#xff0c;表示第i块积木的重量。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>如果能让koko不哭&#xff0c;输出Solo所能获得积木的最大总重量&#xff1b;否则输出“NO”。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;"> <p>3<br /> 3 5 6</p> </td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">11</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>此题中Koko的计算逻辑其实就是按位异或&#xff0c;即两个相应的二进制位值不同则为1&#xff0c;否则为0。</p> 
<p>因此&#xff0c;如果我们想按Koko的求和逻辑平分总重量的话&#xff0c;必然要生成两份相同的二进制数重量&#xff0c;而两个相同二进制数按位异或的结果就是0。</p> 
<p>因此我们只要按位异或所有重量&#xff0c;最终结果为0的话&#xff0c;才能按照Koko的逻辑平分总重量。</p> 
<p>而一旦可以平分总重量&#xff0c;则减去任意一个重量&#xff0c;都可以分成两份相同的二进制数&#xff0c;而为了使Solo能分得最大重量&#xff0c;则必然减去一个最轻的给Koko。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 2) {
    const n &#61; parseInt(lines[0]);
    const weights &#61; lines[1].split(&#34; &#34;).map(Number).slice(0, n);

    console.log(getSoloMaxWeight(weights));

    lines.length &#61; 0;
  }
});

function getSoloMaxWeight(weights) {
  // 升序排序
  weights.sort((a, b) &#61;&gt; a - b);

  const min &#61; weights[0];

  // correctSum记录Solo计算的正确的总重量
  let correctSum &#61; min;
  // faultSum记录Koko计算的错误的总重量
  let faultSum &#61; min;

  for (let i &#61; 1; i &lt; weights.length; i&#43;&#43;) {
    correctSum &#43;&#61; weights[i];
    // Koko的计算方法其实就是二进制按位异或运算&#xff0c;即如果两个相应的二进制位值不同则为1&#xff0c;否则为0。
    faultSum ^&#61; weights[i];
  }

  // 如果按照Koko计算方法&#xff0c;若想按重量平分&#xff0c;必然会生成两份相同的二进制数&#xff0c;而两个相同二进制数&#xff0c;按位异或的结果必然是0
  if (faultSum &#61;&#61;&#61; 0) {
    return correctSum - min; // faultSum&#61;0表示可以平分&#xff0c;因此任意减去一个重量&#xff0c;都可以得到两个相同的二进制数&#xff0c;因此就减去最小的&#xff0c;这样Solo就可以分得最重的
  } else {
    return &#34;NO&#34;; // faultSum !&#61; 0 表示无法按照Koko的逻辑平分
  }
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int n &#61; sc.nextInt();

    int[] weights &#61; new int[n];
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) weights[i] &#61; sc.nextInt();

    System.out.println(getResult(n, weights));
  }

  public static String getResult(int n, int[] weights) {
    // 升序
    Arrays.sort(weights);

    int min &#61; weights[0];

    // correctSum记录Solo计算的正确的总重量
    int correctSum &#61; min;
    // faultSum记录Koko计算的错误的总重量
    int faultSum &#61; min;

    for (int i &#61; 1; i &lt; weights.length; i&#43;&#43;) {
      correctSum &#43;&#61; weights[i];
      // Koko的计算方法其实就是二进制按位异或运算&#xff0c;即如果两个相应的二进制位值不同则为1&#xff0c;否则为0。
      faultSum ^&#61; weights[i];
    }

    // 如果按照Koko计算方法&#xff0c;若想按重量平分&#xff0c;必然会生成两份相同的二进制数&#xff0c;而两个相同二进制数&#xff0c;按位异或的结果必然是0
    if (faultSum &#61;&#61; 0) {
      // faultSum&#61;0表示可以平分&#xff0c;因此任意减去一个重量&#xff0c;都可以得到两个相同的二进制数&#xff0c;因此就减去最小的&#xff0c;这样Solo就可以分得最重的
      return correctSum - min &#43; &#34;&#34;;
    } else {
      // faultSum !&#61; 0 表示无法按照Koko的逻辑平分
      return &#34;NO&#34;;
    }
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n &#61; int(input())
weights &#61; list(map(int, input().split()))


# 算法入口
def getResult(n, weights):
    weights.sort()

    minV &#61; weights[0]

    correctSum &#61; minV
    faultSum &#61; minV

    for i in range(1, n):
        correctSum &#43;&#61; weights[i]
        faultSum ^&#61; weights[i]

    if faultSum &#61;&#61; 0:
        return str(correctSum - minV)
    else:
        return &#34;NO&#34;


# 算法调用
print(getResult(n, weights))
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

void getResult(int n, int weights[]);

int main() {
    int n;
    scanf(&#34;%d&#34;, &amp;n);

    int weights[n];
    for(int i&#61;0; i&lt;n; i&#43;&#43;) {
        scanf(&#34;%d&#34;, &amp;weights[i]);
    }

    getResult(n, weights);

    return 0;
}

int cmp(const void* a, const void* b) {
    return (*(int*) a) - (*(int*) b);
}

void getResult(int n, int weights[]) {
    qsort(weights, n, sizeof(int), cmp);

    int min &#61; weights[0];

    int correctSum &#61; min;
    int faultSum &#61; min;

    for(int i&#61;1; i&lt;n; i&#43;&#43;) {
        correctSum &#43;&#61; weights[i];
        faultSum ^&#61; weights[i];
    }

    if(faultSum &#61;&#61; 0) {
        printf(&#34;%d\n&#34;, correctSum - min);
    } else {
        puts(&#34;NO&#34;);
    }
}</code></pre> 
<p> </p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>